package com.fitso.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.fitso.model.bean.User;
import com.fitso.model.service.user.UserService;
import com.fitso.web.Constants;
import com.fitso.web.bean.Credentials;

/**
 * <p>
 * Controller for the {@link User} model. Logs in, logs out, adds new and edits
 * users.
 * </p>
 * 
 * @author timothystorm
 */
@Controller
@SessionAttributes("user")
public class UserController {
	private UserService _userService;

	public void setUserService(UserService userService) {
		_userService = userService;
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(HttpServletRequest request) {
		User user = getSessionUser(request);

		if (user == null) {
			return new ModelAndView("/users/login", "credentials", new Credentials());
		}
		return new ModelAndView("/users/show", "user", user);
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@ModelAttribute("credentials") Credentials credentials, BindingResult result,
			HttpServletRequest request) {
		User user = _userService.fetch(credentials.getEmail(), credentials.getPassword());

		if (user != null) {
			setSessionUser(request, user);
		} else {
			result.reject("invalid.login", "Invalid Login");
			return new ModelAndView("/users/login", "credentials", new Credentials());
		}
		return new ModelAndView("/users/show", "user", user);
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		setSessionUser(request, null);
		return "redirect:/users/login";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView add() {
		return new ModelAndView("/users/add", "user", new User());
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(HttpServletRequest request, @ModelAttribute("user") User user) {
		_userService.add(user);
		setSessionUser(request, user);
		return new ModelAndView("/users/show", "user", user);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public Object edit(HttpServletRequest request) {
		User user = getSessionUser(request);

		if (user == null) {
			return "redirect:/users";
		}
		return new ModelAndView("/users/edit", "user", user);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(HttpServletRequest request, @ModelAttribute("user") User user) {
		_userService.edit(user);
		setSessionUser(request, user);
		return new ModelAndView("/users/show", "user", user);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(HttpServletRequest request) {
		User user = getSessionUser(request);

		if (user != null) {
			_userService.delete(user);
			setSessionUser(request, null);
		}
		return "redirect:/users";
	}

	public static User getSessionUser(HttpServletRequest request) {
		return (User) WebUtils.getSessionAttribute(request, Constants.SESSION_USER);
	}

	public static void setSessionUser(HttpServletRequest request, User user) {
		WebUtils.setSessionAttribute(request, Constants.SESSION_USER, user);
	}
}
